package com.example.springfreemarker.controller;

import cn.hutool.core.util.RandomUtil;
import com.example.springfreemarker.pojo.dayincome.ComboIncomeDetail;
import com.example.springfreemarker.pojo.dayincome.DayRevenue;
import com.example.springfreemarker.pojo.dayincome.IncomeDetail;
import com.example.springfreemarker.pojo.dayincome.Revenue;
import com.example.springfreemarker.pojo.duty.DayDuty;
import com.example.springfreemarker.pojo.duty.OperRecord;
import com.example.springfreemarker.pojo.duty.ParkRecord;
import com.example.springfreemarker.pojo.duty.Watchman;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;

@Controller
public class AppController {
    @Autowired
    private Configuration configuration;

    private DayRevenue dayRevenue;
    private DayDuty dayDuty;

    @PostConstruct
    public void init() {
        dayRevenue = DayRevenue.builder()
                .estateRevenue(Revenue.builder()
                        .gzBank("234.00")
                        .total(IncomeDetail.builder().discount("100.21")
                                .notTake("23.00")
                                .real("82.23")
                                .refund("99.12").build())
                        .subjectName("xxjqr物业")
                        .monthCardDetail(ComboIncomeDetail.detailBuilder()
                                .expired("3")
                                .newCard("20")
                                .build())
                        .oppositeTempOnline(IncomeDetail.builder()
                                .discount("88.00")
                                .notTake("121.00")
                                .order("55.13")
                                .real("99.01")
                                .refund("1899.98")
                                .build())
                        .build())
                .parkRevenues(new ArrayList<Revenue>() {
                    {
                        add(Revenue.builder()
                                .subjectName("xxjqr停车场")
                                .monthCardDetail(ComboIncomeDetail.detailBuilder()
                                        .chargeCardConsume("23.21")
                                        .newCard("34.11")
                                        .build())
                                .build());
                        add(Revenue.builder()
                                .subjectName("xxjqr2停车场")
                                .build());
                    }
                }).build();


        dayDuty = DayDuty.builder()
                .estateName("妈卖批物业")
                .totalRecord(OperRecord.builder()
                        .exceptionNum(RandomUtil.randomNumbers(2))
                        .notTakeIncome(RandomUtil.randomNumbers(2))
                        .realIncome(RandomUtil.randomNumbers(2))
                        .repealNum(RandomUtil.randomNumbers(2))
                        .shouldIncome(RandomUtil.randomNumbers(2))
                        .build()
                )
                .parkRecords(new ArrayList<ParkRecord>() {
                    {
                        for (int i = 0; i < 10; i++) {
                            add(ParkRecord.builder()
                                    .name("停车场" + i)
                                    .parkOperRecord(new OperRecord(RandomUtil.randomNumbers(2)
                                            , RandomUtil.randomNumbers(2)
                                            , RandomUtil.randomNumbers(2)
                                            , RandomUtil.randomNumbers(2)
                                            , RandomUtil.randomNumbers(2)))
                                    .watchmans(new ArrayList<Watchman>() {
                                        {
                                            for (int i = 0; i < 5; i++) {
                                                add(new Watchman(RandomUtil.randomString(4), new OperRecord(RandomUtil.randomNumbers(2)
                                                        , RandomUtil.randomNumbers(2)
                                                        , RandomUtil.randomNumbers(2)
                                                        , RandomUtil.randomNumbers(2)
                                                        , RandomUtil.randomNumbers(2))
                                                ));
                                            }
                                        }
                                    })
                                    .exceptions(new ArrayList(){
                                        {
                                            for (int i = 0; i < 5; i++) {
                                                add(new HashMap<String,String>(){
                                                    {
                                                        put("name", RandomUtil.randomString(4));
                                                        put("num", RandomUtil.randomNumbers(1));
                                                    }
                                                });
                                            }
                                        }
                                    })
                                    .build());
                        }
                    }
                }).build();
    }

    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws Exception {

        HashMap<String, Object> map = new HashMap<>();
        map.put("dayRevenue", dayRevenue);
        map.put("dayDuty", dayDuty);
        //构造输出流
        Template template = configuration.getTemplate("复杂excel.ftl", "UTF-8");
        String fileName = "C:\\Users\\xxjqr\\Desktop\\MFiles\\" + "tmp" + ".xlsx";
        File file = new File(fileName);
        FileWriter out = new FileWriter(fileName);
        //变量替换
        template.process(map, out);
        //将文件输出到response,返回给客户端
        FileInputStream in = new FileInputStream(file);
        byte[] buffer = new byte[in.available()];
        in.read(buffer);
        in.close();
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=file.xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/octet-stream");
        outputStream.write(buffer);
        outputStream.flush();
        outputStream.close();
    }
}
